package com.colorsstudio.defence.Levels
{
	import com.colorsstudio.defence.ConstantValues.Constants;
	import com.colorsstudio.defence.GameMain;
	import com.greensock.TweenMax;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import idv.cjcat.fenofsm.events.StateEvent;
	import idv.cjcat.fenofsm.FSMachine;
	import idv.cjcat.fenofsm.State;
	
	/**
	 * ...
	 * @author wuzhida
	 */
	public class Level extends Sprite
	{
		public var state:State;
		public var levels:Dictionary = Constants.levels;
		public var flag:Boolean;
		public var layer1:Sprite = new Sprite();
		public var layer2:Sprite = new Sprite();
		public var layer3:Sprite = new Sprite();
		public var layer4:Sprite = new Sprite();
		
		public function Level()
		{
			setState();
		}
		
		public function init():void
		{
			this.addChild(layer1);
			this.addChild(layer2);
			this.addChild(layer3);
			this.addChild(layer4);
		}
		
		public function setState():void
		{
			state.addEventListener(StateEvent.ENTER, levelStart);
			state.addEventListener(StateEvent.EXIT, levelEnd);
		}
		
		public function levelEnd(e:StateEvent):void
		{
			dispose();
		}
		
		public function levelStart(e:StateEvent):void
		{
			init();
		}
		
		public function mainMenu():void
		{
			
		}
		
		public function pauseGame():void
		{
			
		}
		public function restartGame():void
		{
			
		}		
		public function dispose():void
		{
			var numChildren:int = 0;
			numChildren = layer1.numChildren;
			for (var i1:int = 0; i1 < numChildren; i1++)
			{
				this.layer1.removeChildAt(0);
			}
			numChildren = layer2.numChildren;
			for (var i2:int = 0; i2 < numChildren; i2++)
			{
				this.layer2.removeChildAt(0);
				trace(layer2.numChildren);
			}
			numChildren = layer3.numChildren;
			for (var i3:int = 0; i3 < numChildren; i3++)
			{
				this.layer3.removeChildAt(0);
			}
			numChildren = layer4.numChildren;
			for (var i4:int = 0; i4 < numChildren; i4++)
			{
				this.layer4.removeChildAt(0);
			}
			//这里不可以把这些置为null，因为在状态返回的时候，我们没有new整个level，所以这些变量是不可以为空的，一旦为空，比如说layer1为空，我们就不可以在layer上addChild
			//layer1 = layer2 = layer3 = layer4 = null;
			//state = null;			
			trace("Level Clear! Wait for gabage collection!");
		}
	}

}